package com.hs.biye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.mapper.EmployeeMapper;
import com.hs.biye.model.DTO.SalaryRequest;
import com.hs.biye.model.VO.SalaryVo;
import com.hs.biye.model.entity.Employee;
import com.hs.biye.model.entity.Salary;
import com.hs.biye.service.SalaryService;
import com.hs.biye.mapper.SalaryMapper;
import com.sun.org.glassfish.gmbal.ManagedAttribute;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author haoshuai
* @description 针对表【salaryId】的数据库操作Service实现
* @createDate 2023-12-14 18:42:48
*/
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary>
    implements SalaryService {

    @Resource
    private SalaryMapper salaryMapper;


    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 查询所有的薪资信息
     * @return
     */
    public List<SalaryVo> getSalaries(){
        List<Salary> salaryList = salaryMapper.selectList(null);
        List<SalaryVo> salaryVoList = new ArrayList<>();

        salaryList.forEach(item->{
            SalaryVo salaryVo = new SalaryVo();
            BeanUtils.copyProperties(item,salaryVo);
            salaryVoList.add(salaryVo);
        });

        return salaryVoList;
    }

    /**
     * 获取薪资的总价和类型
     * @return
     */
    public List<Salary> getSalaryTotalAndType(){
        LambdaQueryWrapper<Salary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Salary::getTotal,Salary::getSlarayId);
        List<Salary> salaryList = salaryMapper.selectList(lambdaQueryWrapper);
        return salaryList;
    }


    /**
     * 添加薪资
     * @param salaryRequest
     * @return
     */
    public boolean addSalary(SalaryRequest salaryRequest){
        if (salaryRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Salary salary = new Salary();
        BeanUtils.copyProperties(salaryRequest,salary);
        salary.setTotal(salary.getBasicSalary() + salary.getAllowance());
        return  save(salary);
    }


    /**
     * 删除薪资
     * @param id
     * @return
     */
    public  boolean deleteSalary(Integer id){
        if (id == null){
            return false;
        }

        // 删除之前判断是否有员工使用该类型的薪资标准
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getSalaryType,id);
        Long res = employeeMapper.selectCount(queryWrapper);
        if (res > 0) {
            return false;
        }
        return  removeById(id);
    }


    //修改薪资
    public  boolean updateSalary(SalaryRequest salaryRequest){
        if (salaryRequest == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Salary salary = new Salary();
        BeanUtils.copyProperties(salaryRequest,salary);
        salary.setTotal(salary.getBasicSalary() + salary.getAllowance());
        return updateById(salary);
    }


    //查询单个
    @Override
    public SalaryVo getSalariyById(Integer id) {
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Salary salary = getById(id);
        SalaryVo salaryVo = new SalaryVo();
        BeanUtils.copyProperties(salary,salaryVo);
        return salaryVo;
    }
}




